<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">// <![CDATA[
function imgChange(obj1, obj2) {
            var z_photo = $('.z_photo')[0];
            var z_photo_length = z_photo.children.length - 1;
            
            if (z_photo_length >= 3){
                alert('预览图片不能大于3张！！！')
                return;
            } 
            //z_photo.style.width = z_photo_length * 50 + 288 + 'px';
            
            //获取点击的文本框
            var file = document.getElementById("file");
            //存放图片的父级元素
            var imgContainer = document.getElementsByClassName(obj1)[0];
            //获取的图片文件
            var fileList = file.files;
            //文本框的父级元素
            var input = document.getElementsByClassName(obj2)[0];
            var imgArr = [];
            var img;
            //遍历获取到得图片文件
            
            if (fileList.length > 3){
                alert('预览图片不能大于3张！！！')
                return;
            } 
            for (let i = 0; i < fileList.length; i++) {
                // var imgUrl = window.URL.createObjectURL(file.files[i]);

               let reader = new FileReader();
               var imgUrl = reader.readAsDataURL( file.files[i] )
               reader.onload = (e)=>{
                     //base64加密发送到后台
                    let result = e.target.result.replace(/data\:image\/\w.+;base64,/, '')

                     $.ajax({
                    type: 'post',
                    url: '<%=basePath%>BACKEND/uploadtest1_upload',//发送到后台的请求路径
                    dataType: 'json',
                    data: {
                        suju: result
                    },
                    success: (res) => {
                          //获取后台传来的删除预览图的地址
                          var deletePath = res.files[0].delete_url;
                           //获取后台预览的图片
                          var srcUrl = res.files[0].url.replace(/\\/g, '/');
                          console.log(srcUrl);
                          let len = i;
                          if( len === 0 ){
                              len = z_photo_length;
                          }
                          //把数据写入文本款  便于提交页面发送到后端
                          var saveImgPath = document.createElement("input");
                          saveImgPath.value = imgPath1;
                          saveImgPath.type = 'hidden';
                          saveImgPath.name = 'imgPojoList['+ len +'].t_itemUrl';
                          
                          var teq = document.createElement("input");
                          teq.value = z_photo_length;
                          teq.type = 'hidden';
                          teq.name = 'imgPojoList['+ len +'].t_seq';
                          
                          var t_itemType = document.createElement("input");
                          t_itemType.value = "2";
                          t_itemType.type = 'hidden';
                          t_itemType.name = 'imgPojoList['+ len +'].t_itemType';
                          //添加预览图
                          var img = document.createElement("img");
                          img.setAttribute("data-delePath", deletePath );
                          img.setAttribute("src", imgPath );
                          //添加删除预览图图表
                          var clearIco = document.createElement("span");
                          clearIco.setAttribute("class", "delete_parent");
                          clearIco.innerHTML = 'X';
                          
                          
                          var imgAdd = document.createElement("div");
                          imgAdd.setAttribute("class", "z_addImg");
                          imgAdd.appendChild(clearIco);
                          imgAdd.appendChild(saveImgPath);
                          imgAdd.appendChild(teq);
                          imgAdd.appendChild(t_itemType);
                          imgAdd.appendChild(img);
             $('.z_photo')[0].insertBefore(imgAdd, $('.z_file')[0] );
                    
                        imgRemove();
                    },
                    error: (err) =>{
                        console.log( err.status )
                    }
                })
               } 
            };

            
        };
          //删除预览图
        function imgRemove() {
            var imgList = document.getElementsByClassName("z_addImg");
            var mask    = document.getElementsByClassName("z_mask")[0];
            var cancel  = document.getElementsByClassName("z_cancel")[0];
            var sure    = document.getElementsByClassName("z_sure")[0];

            $( imgList ).on('click','.delete_parent',function(e){
                  var _self = $(this);    
                   _self.parent().animate({
                       'margin-top': '40px',
                       'opacity': 0
                   },function(){
                       $(this).remove();
                   })
              
               var delPath = _self.siblings('img').data('delepath');
               $.ajax({
                    type: 'post',
                    url: '<%=basePath%>BACKEND/delUploadtest1_upload',
                    dataType: 'json',
                    data: {
                        delSuju: delPath
                    },
                    success: (res) => {
                         
                    },
                    error: (err) =>{
                        console.log( err.status );
                    }
                })

            })
        };
// ]]></script>